package com.hermes.biz.dao.item.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hermes.biz.common.model.item.ItemDO;

import com.hermes.biz.dao.item.ItemDAO;

public class ItemDAOImpl extends SqlMapClientDaoSupport implements ItemDAO{

	private Logger log = Logger.getLogger(ItemDAOImpl.class);
	
	/**
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ItemDO> getItemList(long start , long end) {
		
		HashMap<Object, Object> map =new HashMap<Object, Object>();
		map.put("start", start);
		map.put("end",end);
		map.put("count",end-start);
		
		try {
			return this.getSqlMapClientTemplate().getSqlMapClient().queryForList("ItemDAO.getAllItems",map);
		} catch (SQLException e) {
			
			log.error(e);
		}
		return null;
	}

	/**
	 * 
	 * @param itemName
	 * @return
	 */
	public ItemDO getItemByItemName(String itemName) {
		 try {
			return (ItemDO)this.getSqlMapClientTemplate().getSqlMapClient().queryForObject("ItemDAO.getItemByName",itemName);
		} catch (SQLException e) {
			log.error(e);
		}
		return null;
	}

	/**
	 * 
	 * @param itemId
	 * @return
	 */
	public ItemDO getItemByItemId(long itemId) {
		try {
			return (ItemDO)this.getSqlMapClientTemplate().getSqlMapClient().queryForObject("ItemDAO.getItemById",itemId);
		} catch (SQLException e) {
			log.error(e);
		}
		return null;
	}

	/**
	 * 
	 * @param itemDO
	 */
	public void saveItem(ItemDO itemDO) {
		 try {
			this.getSqlMapClientTemplate().getSqlMapClient().insert("ItemDAO.insertItem",itemDO);
		} catch (SQLException e) {
			log.error(e);
		}
	}

	/**
	 * ɾ����Ʒ��Ϣ
	 * @param itemId
	 */
	public void deleteItemByItemId(long itemId) {
		try {
			this.getSqlMapClientTemplate().getSqlMapClient().delete("ItemDAO.deleteItem", itemId);
		} catch (SQLException e) {
			log.error(e);
		}
	}

	/**
	 * 
	 * @param itemDO
	 */
	public void updateItem(ItemDO itemDO) {
		try {
			this.getSqlMapClientTemplate().getSqlMapClient().update("ItemDAO.updateItem", itemDO);
		} catch (SQLException e) {
			log.error(e);
		}
	}

	/**
	 * 
	 * @param itemDO
	 */
	public void updateItemLovedNumber(ItemDO itemDO) {
		try {
			this.getSqlMapClientTemplate().getSqlMapClient().update("ItemDAO.updateItemLovedNum", itemDO);
		} catch (SQLException e) {
			log.error(e);
		}		
	}

	/**
	 * 
	 * @param itemDO
	 */
	public void updateItemClickNumber(ItemDO itemDO) {
		try {
			this.getSqlMapClientTemplate().getSqlMapClient().update("ItemDAO.updateItemClickNum", itemDO);
		} catch (SQLException e) {
			log.error(e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<ItemDO> getItemByItemTagId(long tagId,long start , long end) {

		HashMap<Object, Object> map =new HashMap<Object, Object>();
		map.put("start", start);
		map.put("end",end);
		map.put("tagId",tagId);
		map.put("count",end-start);
		
		try {
			return (List<ItemDO>)this.getSqlMapClientTemplate().getSqlMapClient().queryForList("ItemDAO.getHomePageItemByTagId",map);
		} catch (SQLException e) {
			log.error(e);
		}
		return null;
	}

	public long countItemNumber(long tagId) {
		try {
			return (Long)this.getSqlMapClientTemplate().getSqlMapClient().queryForObject("ItemDAO.countItem",tagId);
		} catch (SQLException e) {
			log.error(e);
		}
		return 0;
	}

	public long countIndexPageItemNumber() {
		try {
			return (Long)this.getSqlMapClientTemplate().getSqlMapClient().queryForObject("ItemDAO.countIndexItem");
		} catch (SQLException e) {
			log.error(e);
		}
		return 0;
	}



}
